package controllers;

import java.util.TimerTask;

import controllers.SendMailSSL.SendMailRunnable;

import Entity.BorrowRecored;
import Entity.User;

import messagesPacks.BorrowAndUserMessage;

/**
 * This class starts a new timed thread that checks the borrows table and sends
 * mail to subscribers that need to return their books the next day.
 */

public class JobCheckBorrows extends TimerTask 
{
	private DBController db_ref = null;
	private EchoServer echo_ref = null;
	private IntSysClock clk_ref = null;
	
	/**
	 * Sets up the timer thread.
	 * @author Alon Shahaf
	 * @Data 12.01.2013
	 * @param db	a reference to the DB controller
	 * @param echo  a reference to the communication class
	 * @param clk	a reference to the Clock class.
	 */
	public JobCheckBorrows(DBController db, EchoServer echo, IntSysClock clk)
	{
		this.setDb_ref(db);
		this.setEcho_ref(echo);
		this.setClk_ref(clk);
	}
	
	public DBController getDb_ref() 
	{
		return db_ref;
	}

	public void setDb_ref(DBController db_ref) 
	{
		this.db_ref = db_ref;
	}
	
	public void setClk_ref(IntSysClock clk_ref) 
	{
		this.clk_ref = clk_ref;
	}
	
	public void setEcho_ref(EchoServer echo_ref) 
	{
		this.echo_ref = echo_ref;
	}
	
	/**
	 * Scans all the active borrows and sends mail to users that needs to return their books the next day.
	 * @author Alon Shahaf
	 * @Data 12.01.2013
	 */
    public void run() 
    {
    	System.out.println("(JOB - JobCheckBorrows) "+clk_ref.getTime().toString()+ " Running..");
   		BorrowAndUserMessage data;
   		// Get all users that their borrows that ends in the next day.
   		data = db_ref.get1DayLeftBorrows(clk_ref.getTime());
   		if ((data != null) && (data.hasGoodData()))
   		{
   			// Define N threads
   			Thread [] t = new Thread[data.getCounter()+1];
   			
   			// Prepare subject message
   			String subject = "OBL System - Copy Return Reminder";
   			int i=0;
   			// Send Email for each of those users
   			for (i=0; i<= data.getCounter(); i++)
   			{
				BorrowRecored br = data.getBorrow(i);
				User u = data.getUser(i);
				int bn = br.borrowNumber; 
				String copy_id = br.copyID;
				String start_date = br.borrowDate;
				String end_date = br.returnDate;
				String id = u.getID();
				String first_name = u.getFname();
				String last_name = u.getLname();
				String email = u.getEmail();
				String phone = u.getPhone();
				
				System.out.println("(JOB - JobCheckBorrows) ("+i+"/"+data.getCounter()+") Borrow Reminder for ID="+id+" Copy-ID="+copy_id);
				
				// Prepare body message
				String body = "Dear "+first_name+"," +
						"\n\n" +
						"Your borrow ends tommorow!\n" +
						"Borrow details:\n"+
						"Borrow Number - " + bn + "\n" +
						"Copy ID - " + copy_id + "\n" +
						"Start Date - " + start_date + "\n" +
						"End Date - " + end_date + "\n\n" +
						"Have a nice day,\n" +
						"OBL System.";
				
				// Create the thread
   				t[i] = new Thread(new SendMailRunnable(email,subject,body));
   				
   				// Run the thread
   				t[i].start();
   			} // End of for
   			System.out.println("(JOB - JobCheckBorrows) "+i+" Jobs activated.");
   		}
   		System.out.println("(JOB - JobCheckBorrows) Finished iteration.");
   	}
}
